//
//  CollectTableViewController.m
//  B项目
//
//  Created by lanou on 15/12/25.
//  Copyright © 2015年 tzf. All rights reserved.
//

#import "CollectTableViewController.h"
#import "MainClassTableViewCell.h"
#import "MainClassDetail.h"
#import "MainTwoTableViewCell.h"
#import "WebViewController.h"
@interface CollectTableViewController ()
@property (nonatomic,strong) NSMutableArray *IdArray;

@property (nonatomic,strong) NSMutableArray *TitleArray;

@property (nonatomic,strong) NSMutableArray *ImageArray;

@end

@implementation CollectTableViewController
static bool ison = YES;

- (NSMutableArray *)IdArray{
    if (!_IdArray) {
        _IdArray = [[NSMutableArray alloc]init];
    }
    return _IdArray;
}
- (NSMutableArray *)TitleArray{
    if (!_TitleArray) {
        _TitleArray = [[NSMutableArray alloc]init];
    }
    return _TitleArray;
}
- (NSMutableArray *)ImageArray{
    if (!_ImageArray) {
        _ImageArray = [[NSMutableArray alloc]init];
    }
    return _ImageArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    ison = YES;
    self.view.backgroundColor = [UIColor whiteColor];

    self.view.backgroundColor = [UIColor whiteColor];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data1 = [defaults objectForKey:@"id"];
    NSData *data2 = [defaults objectForKey:@"title"];
    NSData *data3 = [defaults objectForKey:@"image"];

    self.IdArray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data1];
    self.TitleArray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data2];
    self.ImageArray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data3];
    
    self.title = @"收藏的攻略";
    
    
    [self.tableView registerClass:[MainClassTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[MainTwoTableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(change)];
    


}
- (void)change{
    if (ison == YES) {
        ison = NO;
        [self.tableView reloadData];
    }else{
        ison = YES;
        [self.tableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.IdArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (ison == YES) {
        MainClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        MainClassDetail *mainDetail = [[MainClassDetail alloc]init];
        mainDetail.ID = self.IdArray[indexPath.row];
        mainDetail.cover_image_url = self.ImageArray[indexPath.row];
        mainDetail.title = self.TitleArray[indexPath.row];
        
        cell.mainDetail = mainDetail;
        
        return cell;

    }else{
        MainTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        MainClassDetail *mainDetail = [[MainClassDetail alloc]init];
        mainDetail.ID = self.IdArray[indexPath.row];
        mainDetail.cover_image_url = self.ImageArray[indexPath.row];
        mainDetail.title = self.TitleArray[indexPath.row];
        
        cell.mainDetail = mainDetail;
        
        return cell;

        
        
    }
   }





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (ison == YES) {
//        WebViewController *webvc = [[WebViewController alloc]init];
//        ChooseGift *choose = [[ChooseGift alloc]init];
//        choose = self.modelArray[indexPath.row];
//        webvc.userid = choose.ID;
//        webvc.style = 100;
//        [webvc setHidesBottomBarWhenPushed:YES];
//        [self.navigationController pushViewController:webvc animated:YES];

        WebViewController *web = [[WebViewController alloc]init];
        
        web.userid = [NSString stringWithFormat:kSearchDetail,self.IdArray[indexPath.row]];
        web.style = 100;
        [web setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:web animated:YES];
        web.type = 2;

    
    }else{
        
        [NSUserDefaults standardUserDefaults];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"id"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"title"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"image"];

        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.IdArray removeObject:self.IdArray[indexPath.row]];
        [self.TitleArray removeObject:self.TitleArray[indexPath.row]];
        [self.ImageArray removeObject:self.ImageArray[indexPath.row]];

        NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:_IdArray];
        NSData *data2 = [NSKeyedArchiver archivedDataWithRootObject:_TitleArray];
        NSData *data3 = [NSKeyedArchiver archivedDataWithRootObject:_ImageArray];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:data1 forKey:@"id"];
        [defaults setObject:data2 forKey:@"title"];
        [defaults setObject:data3 forKey:@"image"];

        [self.tableView reloadData];
        
    }

}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
